package com.qin.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.mall.domain.PmsBrand;
import com.qin.mall.mapper.PmsBrandMapper;
import com.qin.mall.dto.BrandDto;
import com.qin.mall.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【pms_brand(品牌表)】的数据库操作Service实现
* @createDate 2022-11-05 11:33:47
*/
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand>
    implements PmsBrandService{
    
    @Autowired
    private PmsBrandMapper brandMapper;

    //TODO 查询所有品牌
    @Override
    public List<PmsBrand> listAllBrand() {
        List<PmsBrand> brands = brandMapper.selectList(null);
        return brands;
    }

    //TODO 创建新的品牌
    @Override
    public int createBrand(BrandDto brandDto) {
        int insert = brandMapper.insert(brandDto);
        return insert;
    }

    // TODO 根据id更新品牌
    @Override
    public int updateBrand(Long id, BrandDto brandDto) {
        //更新的条件
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        int update = brandMapper.update(brandDto,wrapper);
        return update;
    }

    // TODO 通过id删除品牌
    @Override
    public int deleteBrand(Long id) {
        int result = brandMapper.deleteById(id);
        return result;
    }

    //TODO 批量删除
    @Override
    public int deleteBrands(List<Long> ids) {
        int result = brandMapper.deleteBatchIds(ids);
        return result;
    }

    //TODO 品牌分页列表
    @Override
    public Page<PmsBrand> listBrand(String keyword, int pageNum, int pageSize) {
        //设置分多少页和每页大小
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        Page<PmsBrand> pages = brandMapper.selectPage(page, wrapper);
        return pages;
    }

    //TODO 通过id获取品牌
    @Override
    public PmsBrand getBrand(Long id) {
        PmsBrand brand = brandMapper.selectById(id);
        return brand;
    }

    //TODO 批量更新显示状态
    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        PmsBrand brand = new PmsBrand();
        //列id 在ids集合里面
        wrapper.in("id",ids);
        brand.setShowStatus(showStatus);
        int update = brandMapper.update(brand, wrapper);
        return update;
    }

    //TODO 批量更新厂家制造商状态
    @Override
    public int updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        PmsBrand brand = new PmsBrand();
        wrapper.in("id",ids);
        brand.setFactoryStatus(factoryStatus);
        int update = brandMapper.update(brand, wrapper);
        return update;
    }
}




